require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIEquipFilter = {};
HelperFunc.SetUIEnv(UIEquipFilter)
local this = UIEquipFilter;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.btnBgClose = nil;
this.txtLevel = nil;
this.btnLevelReduct = nil;
this.btnLevelAdd = nil;
this.txtQuality = nil;
this.btnQualityReduct = nil;
this.btnQualityAdd = nil;
this.btnConfirm = nil;

this.funcConfirm = nil;
this.curFilterLevel = nil;
this.curFilterQuality = nil;

local filterLevel = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 };
local filterQuality = { 0, 2, 3, 4 };

function UIEquipFilter.Init(func, filter)
    --todo 应该小写 暂时先大写
    funcConfirm = func;
    if filter ~= nil and filter.level ~= nil then
        curFilterLevel = filter.level;
    else
        curFilterLevel = filterLevel[2];
    end

    if filter ~= nil and filter.quality ~= nil then
        curFilterQuality = filter.quality;
    else
        curFilterQuality = filterQuality[2];
    end

    local name, packageName = getUIName(PanelNames.UIEquipFilter);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIEquipFilter.onCreate(obj)
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        btnBgClose = obj:GetChild("BgCloseBtn");
        txtLevel = obj:GetChild("LevelText");
        btnLevelReduct = obj:GetChild("LevelReductBtn");
        btnLevelAdd = obj:GetChild("LevelAddBtn");
        txtQuality = obj:GetChild("QualityText");
        btnQualityReduct = obj:GetChild("QualityReductBtn");
        btnQualityAdd = obj:GetChild("QualityAddBtn");
        btnConfirm = obj:GetChild("ConfirmBtn");
        this.addEvent();
    end
    this.refresh();
end

function UIEquipFilter.addEvent()
    btnBgClose.onClick:Add(this.onClickClose);
    btnLevelReduct.onClick:Add(this.onClickLevelReduct);
    btnLevelAdd.onClick:Add(this.onClickLevelAdd);
    btnQualityReduct.onClick:Add(this.onClickQualityReduct);
    btnQualityAdd.onClick:Add(this.onClickQualityAdd);
    btnConfirm.onClick:Add(this.onClickConfirm);

end

function UIEquipFilter.removeEvent()
    btnBgClose.onClick:Clear();
    btnLevelReduct.onClick:Clear();
    btnLevelAdd.onClick:Clear();
    btnQualityReduct.onClick:Clear();
    btnQualityAdd.onClick:Clear();
    btnConfirm.onClick:Clear();

end

function UIEquipFilter.LocalListenEvent()

end

function UIEquipFilter.LocalCloseEvent()

end

function UIEquipFilter.OnDestroy()
    this.removeEvent();
end

function UIEquipFilter.OnEnable()
    this.LocalListenEvent();
end

function UIEquipFilter.OnDisable()
    this.LocalCloseEvent();
end

function UIEquipFilter.refresh()
    txtLevel.text = getLanguage(string.format(Config.LangFilterLevel, curFilterLevel));
    txtQuality.text = getLanguage(string.format(Config.LangFilterQuality, curFilterQuality));
    logWarn("qq:" .. string.format(Config.LangFilterLevel, curFilterLevel));
    logWarn("aa:" .. string.format(Config.LangFilterQuality, curFilterQuality));
end

function UIEquipFilter.onClickClose()
    closeUI(PanelNames.UIEquipFilter);
end

function UIEquipFilter.onClickLevelReduct()
    local index = 0
    for i = 1, #filterLevel do
        if filterLevel[i] == curFilterLevel then
            index = i;
        end
    end
    if index > 1 then
        curFilterLevel = filterLevel[index - 1]
    else
        curFilterLevel = filterLevel[#filterLevel];
    end
    this.refresh();
end

function UIEquipFilter.onClickLevelAdd()
    local index = 0
    for i = 1, #filterLevel do
        if filterLevel[i] == curFilterLevel then
            index = i;
        end
    end
    if index < #filterLevel then
        curFilterLevel = filterLevel[index + 1];
    else
        curFilterLevel = filterLevel[1];
    end
    this.refresh();
end

function UIEquipFilter.onClickQualityReduct()
    local index = 0
    for i = 1, #filterQuality do
        if filterQuality[i] == curFilterQuality then
            index = i;
        end
    end
    if index > 1 then
        curFilterQuality = filterQuality[index - 1]
    else
        curFilterQuality = filterQuality[#filterQuality];
    end
    this.refresh();
end

function UIEquipFilter.onClickQualityAdd()
    local index = 0
    for i = 1, #filterQuality do
        if filterQuality[i] == curFilterQuality then
            index = i;
        end
    end
    if index < #filterQuality then
        curFilterQuality = filterQuality[index + 1];
    else
        curFilterQuality = filterQuality[1];
    end
    this.refresh();
end

function UIEquipFilter.onClickConfirm()
    local data = {
        level = curFilterLevel;
        quality = curFilterQuality;
    }
    funcConfirm(data);
    closeUI(PanelNames.UIEquipFilter);
end

return UIEquipFilter;
